package com.itheima.service.impl;

import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 查询所有的需要统计的数据
 * @Date 2020/10/22 16:33
 */
@Service
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 查询所有的统计数据
     *
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {

        Map<String, Object> map = new HashMap<>();

        //今天的日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //本周一日期
        String firstDayThisWeek = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //本月第一天
        String firstDayThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());


        //今天新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        //今天总会员数
        Integer totalMember = memberDao.findMemberCountBeforeDate(today);
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(firstDayThisWeek);
        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDayThisMonth);


        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountAfterDate(today);
        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(firstDayThisWeek);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(firstDayThisWeek);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDayThisMonth);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDayThisMonth);
        //热门套餐
        List<Map<String,Object>> hotSetmeal = orderDao.findHotSetmeal();
        //获取到每一个套餐
        for (Map m : hotSetmeal) {
            //在套餐后面添加一个%号
            Object proportion =  m.get("proportion")+"%";
            //让有%号的覆盖原来的
            m.put("proportion",proportion);
        }

        map.put("reportDate", today);//今天的时间
        map.put("todayNewMember", todayNewMember); //今天新增会员数
        map.put("totalMember", totalMember); //今天总会员数
        map.put("thisWeekNewMember", thisWeekNewMember);//本周新增会员数
        map.put("thisMonthNewMember", thisMonthNewMember); //本月新增会员数
        map.put("todayOrderNumber", todayOrderNumber);  //今日预约数
        map.put("todayVisitsNumber", todayVisitsNumber); //今日到诊数
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);//本周预约数
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber); //本周到诊数
        map.put("thisMonthOrderNumber", thisMonthOrderNumber); //本月预约数
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);   //本月到诊数
        map.put("hotSetmeal",hotSetmeal);

        return map;
    }
}
